/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SPI_FLASH_H
#define __SPI_FLASH_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
//#include "stm32_eval.h"
/** @addtogroup Utilities
  * @{
  */
  
/** @addtogroup STM32_EVAL
  * @{
  */ 

/** @addtogroup Common
  * @{
  */
  
/** @addtogroup STM32_EVAL_SPI_FLASH
  * @{
  */  

/** @defgroup STM32_EVAL_SPI_FLASH_Exported_Types
  * @{
  */ 
/**
  * @}
  */
  
/** @defgroup STM32_EVAL_SPI_FLASH_Exported_Constants
  * @{
  */
/**
  * @brief  M25P SPI Flash supported commands
  */  
#define sFLASH_CMD_WRITE          0x02  /*!< Write to Memory instruction */
#define sFLASH_CMD_WRSR           0x01  /*!< Write Status Register instruction */
#define sFLASH_CMD_WREN           0x06  /*!< Write enable instruction */
#define sFLASH_CMD_READ           0x03  /*!< Read from Memory instruction */
#define sFLASH_CMD_RDSR           0x05  /*!< Read Status Register instruction  */
#define sFLASH_CMD_RDID           0x9F  /*!< Read identification */
#define sFLASH_CMD_SE             0xD8  /*!< Sector Erase instruction */
#define sFLASH_CMD_BE             0xC7  /*!< Bulk Erase instruction */

#define sFLASH_WIP_FLAG           0x01  /*!< Write In Progress (WIP) flag */

#define sFLASH_DUMMY_BYTE         0xA5
#define sFLASH_SPI_PAGESIZE       0x100

#define sFLASH_M25P128_ID         0x202018
#define sFLASH_M25P64_ID          0x202017
#define sFLASH_M25P32_ID          0x202016		  			//edit by mrzhu
#define sFLASH_M25P16_ID          0x202015					//edit by mrzhu
  
/**
  * @}
  */ 
  
/** @defgroup STM32_EVAL_SPI_FLASH_Exported_Macros
  * @{
  */
/**
  * @brief  Select sFLASH: Chip Select pin low
  */
/**
  * @}
  */ 
#define sFLASH_SPI                       SPI1
#define sFLASH_SPI_CLK                   RCC_APB2Periph_SPI1
#define sFLASH_SPI_SCK_PIN               GPIO_Pin_5                  
#define sFLASH_SPI_SCK_GPIO_PORT         GPIOA                       
#define sFLASH_SPI_SCK_GPIO_CLK          RCC_APB2Periph_GPIOA
#define sFLASH_SPI_MISO_PIN              GPIO_Pin_6                 
#define sFLASH_SPI_MISO_GPIO_PORT        GPIOA                      
#define sFLASH_SPI_MISO_GPIO_CLK         RCC_APB2Periph_GPIOA
#define sFLASH_SPI_MOSI_PIN              GPIO_Pin_7                  
#define sFLASH_SPI_MOSI_GPIO_PORT        GPIOA                      
#define sFLASH_SPI_MOSI_GPIO_CLK         RCC_APB2Periph_GPIOA

#define sFLASH_CS1_PIN                    GPIO_Pin_4                  /* PB.12 */
#define sFLASH_CS1_GPIO_PORT              GPIOA                       /* GPIOB */
#define sFLASH_CS1_GPIO_CLK               RCC_APB2Periph_GPIOA
#define sFLASH_CS2_PIN                    GPIO_Pin_9                  /* PD.13 */
#define sFLASH_CS2_GPIO_PORT              GPIOF                       /* GPIOD */
#define sFLASH_CS2_GPIO_CLK               RCC_APB2Periph_GPIOF
#define sFLASH_CS3_PIN                    GPIO_Pin_10                  /* PC.09 */
#define sFLASH_CS3_GPIO_PORT              GPIOF                       /* GPIOC */
#define sFLASH_CS3_GPIO_CLK               RCC_APB2Periph_GPIOF   


/** @defgroup STM32_EVAL_SPI_FLASH_Exported_Functions
  * @{
  */
/**
  * @brief  High layer functions
  */
void sFLASH_DeInit(void);
void sFLASH_Init(u8 cs);
void sFLASH_EraseSector(u8 cs,uint32_t SectorAddr);
void sFLASH_EraseBulk(u8 cs);
void sFLASH_WritePage(u8 cs,uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite);
void sFLASH_WriteBuffer(u8 cs,uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite);
void sFLASH_ReadBuffer(u8 cs,uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead);
uint32_t sFLASH_ReadID(u8 cs);
void sFLASH_StartReadSequence(u8 cs,uint32_t ReadAddr);

/**
  * @brief  Low layer functions
  */
uint8_t sFLASH_ReadByte(void);
uint8_t sFLASH_SendByte(uint8_t byte);
uint16_t sFLASH_SendHalfWord(uint16_t HalfWord);
void sFLASH_WriteEnable(u8 cs);
void sFLASH_WaitForWriteEnd(u8 cs);
s8 Flash_Test(u8 cs);
#ifdef __cplusplus
}
#endif

#endif  
/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */ 

/**
  * @}
  */  

/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
